<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\SearchRender;
use App\Models\WxSearch;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;

trait formPageSearchTrait{
    public function index_pagesearch(Content $content)
    {
        return $content->title('搜索配置')->description('移动端')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_pagesearch()->render()}</div>
HTML
            );
    }
    protected function form_pagesearch(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        global $__MINI_GLOBAL_TENANT_ID__;

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            $res1 = __system__paycode__tip__(1);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res1['tip']);
            $form->color('app_search_highlight_color', '搜索高亮颜色')->help($res1['text'])->default(Settings::get('app_search_highlight_color', '#fc3a72'));
        }

        $form->multipleSelectTable('app_hot_search', '移动端热搜')
            ->title('搜索记录库')
            ->max(16)
            ->from(SearchRender::make())
            ->model(WxSearch::class, 'id', 'search_content', 'selectTable:formMiniTrait:app_hot_search')
            ->help('<a href="'.admin_url('search').'" target="_blank">搜索记录</a>：如果为空，则选择标记为热门的搜索记录。<a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
            ->default(Settings::get('app_hot_search', '2054,2055,2056,2057,2058,2059,2060,2061,2062,2063,2064,2065,2066,2067,2068', true));
        $form->divider();

        $form->html(function () {
            return '<img src="https://img.mini.minisns.cn/images/hbx/search-hit-word.jpg" style="width: 300px;" />';
        }, '');
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">解释：1、包含匹配是指 搜索关键词包含了触发词 2、一次只会触发一个</div>';
        }, '');
        $form->table('app_search_trigger_word','搜索触发词', function (NestedForm $table) {
            $table->text('text', '触发字')->required();
            $table->select('match', '匹配关系')->options([1=>'绝对匹配', 2=>'包含匹配']);
            $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/images/nav/nav.png" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->uniqueName()->url('files/uploads')->autoUpload();
            $table->text('title', '标题')->required();
            $table->text('desc', '描述');
            $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
            $table->text('target', '跳转id')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
//            $table->sw
        })->default(Settings::get('app_search_trigger_word', '',true));

        $form->action('settings/action/pagesearch');
        return $form;
    }

    public function pagesearch(Request $request){
        global $__MINI_GLOBAL_TENANT_ID__;
        SiteUtils::forget_config_data();

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            Settings::set('app_search_highlight_color', $request->input('app_search_highlight_color'), true, true);
        }

        Settings::set('app_hot_search', AdminUtils::_multipleSelectTable_order_process('selectTable:formMiniTrait:app_hot_search', $request->input('app_hot_search')) , true, true);
        Settings::set('app_search_trigger_word', AdminUtils::_table_data($request->input('app_search_trigger_word')), true, true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
